package com.ljh.dao;

import com.ljh.pojo.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import java.util.List;

public class UserMapperImpl extends HibernateDaoSupport implements UserMapper {
    @Autowired
    private SessionFactory sessionFactory;
    private Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }
    public User queryUserById(String id) {
        List<User> users=getSession().createQuery("from User where id=?").setParameter(0,id).list();
        return users.get(0);
    }

    public User queryUser(User user) {
        List<User> users=getSession().createQuery("from User where id=? and password=?").setParameter(0,user.getId()).setParameter(1,user.getPassword()).list();
        return users.get(0);
    }

    public int addUesr(User user) {
//        getSession().createQuery("insert into User (id,password) values('"+user.getId()+"','"+user.getPassword()+"')").executeUpdate();
        getSession().save(user);
        return 0;
    }
}
